//
//  GSEvent.m
//  AgileNYC
//
//  Created by German on 15.06.14.
//  Copyright (c) 2014 Softlegion. All rights reserved.
//

#import "GSEvent.h"
#import "SGSAppConsts.h"
#import "SGSUtils.h"
#import "SGSServerManagerConsts.h"
#import "RKDotNetDateFormatter.h"

@implementation GSEvent

@dynamic author;
@dynamic avgRating;
@dynamic endTime;
@dynamic eventId;
@dynamic eventImageURL;
@dynamic eventPhoto;
@dynamic introduction;
@dynamic isCheckInChanged;
@dynamic isRatingChanged;
@dynamic isCheckIn;
@dynamic modifiedDate;
@dynamic myRating;
@dynamic room;
@dynamic startTime;
@dynamic topic;

#pragma mark - Event Date and Time to String

-(NSString*) eventTimeRangeInString {
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"h:mma"];
    
    return [NSString stringWithFormat:@"%@ - %@", [formatter stringFromDate:self.startTime],[formatter stringFromDate:self.endTime]];
}


-(NSString*) eventDateInString {
    /*NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd/MM/yyyy"];*/
    
    NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    //NSString * usFormatString = [NSDateFormatter dateFormatFromTemplate:@"dMMMyyyy" options:0 locale:usLocale];
    NSString * usFormatString = [NSDateFormatter dateFormatFromTemplate:@"MMddyyyy" options:0 locale:usLocale];
    
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:usFormatString];
    
    return [NSString stringWithFormat:@"%@", [formatter stringFromDate:self.startTime]];
}

-(void) completeWithServerResponse: (NSDictionary*) response {
    self.eventId            = [response objectForKey:eventIDKey];
    self.topic              = [response objectForKey:eventNameKey];
    self.room               = [response objectForKey:eventTrackKey];
    self.author             = [response objectForKey:eventLecturerNameKey];
    self.introduction       = [response objectForKey:eventDescriptionKey];
    self.eventImageURL      = [response objectForKey:eventPhotoKey];
    self.avgRating          = [response objectForKey:eventAvgRaitingKey];
    self.myRating           = [response objectForKey:eventMyRaitingKey];
    self.isCheckIn          = [response objectForKey:eventIsCheckInKey];
    
    self.eventPhoto         = nil; //Load it on demand

    RKDotNetDateFormatter * dtFormat = [RKDotNetDateFormatter dotNetDateFormatterWithTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    
    self.startTime          = [dtFormat dateFromString:[response objectForKey:eventTimeFromKey]];
    self.endTime            = [dtFormat dateFromString:[response objectForKey:eventTimeToKey]];
    self.modifiedDate       = [dtFormat dateFromString:[response objectForKey:modifiedDateKey]];
}

-(bool) isEventCompleted {
        
    int res = checkDateInRange(self.startTime, [NSDate date], self.endTime);
    
    switch (res) {
        case -1: return NO;
        case 0: return NO;
        case 1: return YES;
        default: return NO;
    }
}

-(bool) isEventInProcess {
    
    int res = checkDateInRange(self.startTime, [NSDate date], self.endTime);
    
    switch (res) {
        case -1: return NO;
        case 0: return YES;
        case 1: return NO;
        default: return NO;
    }

}

-(NSString*) allEventData {
    return [NSString stringWithFormat:@"%@ %@ %@ %@ %@",
            self.author,
            self.topic,
            self.introduction,
            [self eventTimeRangeInString],
            [self eventDateInString]];
}

@end
